package com.ruoyi.project.jxy.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.*;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.jxy.entity.SkillPara;
import com.ruoyi.project.jxy.service.IIntentionService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.jxy.domain.Skill;
import com.ruoyi.project.jxy.service.ISkillService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import springfox.documentation.spring.web.json.Json;

/**
 * 技能Controller
 *
 * @author ruoyi
 * @date 2019-12-06
 */
@RestController
@RequestMapping("/jxy/skill")
public class SkillController extends BaseController {
    @Autowired
    private ISkillService skillService;
    @Autowired
    private IIntentionService intentionService;

    /**
     * 查询技能列表
     */
    @PreAuthorize("@ss.hasPermi('jxy:skill:list')")
    @GetMapping("/list")
    public TableDataInfo list(Skill skill) {
        startPage();
        QueryWrapper<Skill> queryWrapper = new QueryWrapper<Skill>();
        if (StringUtils.isNotBlank(skill.getCode())) {
            queryWrapper.like("code", skill.getCode());
        }
        if (StringUtils.isNotBlank(skill.getName())) {
            queryWrapper.like("name", skill.getName());
        }
        //只能查询本人创建的
        //skill.setCreateUser(SecurityUtils.getUsername());
        queryWrapper.eq("create_user", SecurityUtils.getUsername());
        List<Skill> list = skillService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询技能列表下拉菜单
     */
    @PreAuthorize("@ss.hasPermi('jxy:skill:list')")
    @GetMapping("/listSkill")
    public AjaxResult listSkill(Skill skill) {
        QueryWrapper<Skill> queryWrapper = new QueryWrapper<Skill>();
        //只能查询本人创建的
        skill.setCreateUser(SecurityUtils.getUsername());
        List<Skill> list = skillService.list(queryWrapper);
        return AjaxResult.success(list);
    }

    /**
     * 获取技能详细信息
     */
    @PreAuthorize("@ss.hasPermi('jxy:skill:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(skillService.getById(id));
    }

    /**
     * 新增技能
     */
    @PreAuthorize("@ss.hasPermi('jxy:skill:add')")
    @Log(title = "技能", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Skill skill) {
        if (StringUtils.isBlank(skill.getName())) {
            return AjaxResult.error("技能名称为空");
        }
        if (skill.getSkillType() == null) {
            return AjaxResult.error("技能类型为空");
        }
        //校验
        QueryWrapper checkWrapper = new QueryWrapper();
        checkWrapper.eq("name", skill.getName().trim());
        checkWrapper.eq("create_user", SecurityUtils.getUsername());
        List checkList = skillService.list(checkWrapper);
        if (checkList.size() > 0) {
            return AjaxResult.error("技能名称已存在");
        }
        String code = IdUtil.simpleUUID();
        skill.setCode(code);
        skill.setCreateUser(SecurityUtils.getUsername());
        skill.setCreateTime(new Date());
        skill.setStatus("0");
        skill.setUpdateTime(new Date());
        skill.setStatus("0");
        return toAjax(skillService.save(skill) ? 1 : 0);
    }

    /**
     * 修改技能
     */
    @PreAuthorize("@ss.hasPermi('jxy:skill:edit')")
    @Log(title = "技能", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Skill skill) {
        QueryWrapper checkWrapper = new QueryWrapper();
        checkWrapper.eq("name", skill.getName().trim());
        checkWrapper.eq("create_user", SecurityUtils.getUsername());
        checkWrapper.ne("id", skill.getId());
        List checkList = skillService.list(checkWrapper);
        if (checkList.size() > 0) {
            return AjaxResult.error("技能名称已存在");
        }
        skill.setUpdateTime(new Date());
        skill.setUpdateUser(SecurityUtils.getUsername());
        skill.setStatus("0");
        return toAjax(skillService.updateById(skill) ? 1 : 0);
    }

    @GetMapping("exportSkillFile")
    public void exportSkillFile(Skill skill) {

       /* Map result = skillService.getSkillInfo(skill);
        System.out.println("skill = " + result);*/
        //获取所有意图
        //获取每个意图中对应的语料
        //
    }

    /**
     * @return void
     * @Description //获取技能详细信息
     * @Date 2019/12/30 14:30
     * @Param [socre 阈值, text 文本内容, skillId 技能id]
     **/
    @PostMapping("/skillTest")
    public AjaxResult skillTest(@RequestBody SkillPara skill) {
        logger.info(skill.toString());

        //数据校验，该技能下存在未对应实体的槽位时，给出提示。
        List<Map> checkList = intentionService.checkNullEntityId(skill.getId());
        if (checkList != null && checkList.size() > 0) {
            StringBuffer errorMsg = new StringBuffer();
            for (Map temp :
                    checkList) {
                errorMsg.append("意图code为" + temp.get("code")+"意图名称为"+temp.get("name")+"意图存在未对应的实体。<br/>");
            }
            //errorMsg.append();
            return AjaxResult.error(errorMsg.toString());
        }
        //获取技能详情信息
        Map result = skillService.getSkillInfo(skill);
        System.out.println(JSONObject.toJSONString(result));
        //System.out.println(JSONObject.toJSONString(result));
        //post 发送数据给刘旺,获取返回结果
        System.out.println("skill test data is:" + JSONObject.toJSONString(result));
        String str = HttpRequest.post("http://192.168.2.238:7777/").body(JSONObject.toJSONString(result)).execute().body();
        return AjaxResult.success("获取技能详细信息成功", str);
    }

}
